﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarRace
{
    public class Cloud
    {
        public List<Terminal> terminals;
        public string RichEditData;
        
        public Cloud()
        {
            terminals = new List<Terminal>();
            RichEditData = null;
        }
        public void Send(Terminal from, int id, float x, float y, long time, int mes=0)
        {
            if (id == 1) //My car
            {
                //RichEditData += "SEND POS: " + x.ToString() + "," + y.ToString() + " TIME: " + time.ToString() + "\n";
            }
            foreach(Terminal term in terminals)
            {
                if (Math.Sqrt(Math.Pow(term.current_pos.x - from.current_pos.x, 2) + Math.Pow(term.current_pos.y - from.current_pos.y, 2)) <= 66.66) // 66.66f = 100m
                {
                    if (term.GetType() == typeof(Car))
                    {
                        Car car = (Car)term;
                        if (car != from && car.id == 1) //My car
                        {
                            //RichEditData += "READ ID: " + id.ToString() + " POS: " + x.ToString() + "," + y.ToString() + " TIME: " + time.ToString() + "\n";
                        }
                        if(car != from)
                        {
                            car.Read();
                        }
                    }
                }
            }
        }
        //public void Read(Car car)
        //{
        //    foreach (C2CMessage msg in messages)
        //    {
        //        if(Math.Sqrt(Math.Pow(car.GetPosition().x-msg.x,2) + Math.Pow(car.GetPosition().y-msg.y,2)) <= 66.66) // 66.66f = 100m
        //        {
        //            RichEditData += "READ ID: " + msg.id.ToString() + " POS: " + msg.x.ToString() + "," + msg.y.ToString() + " TIME: " + msg.time.ToString() + "\n";
        //        }
        //    }
        //}
    }
    public class C2CMessage
    {
        public int id;
        public float x;
        public float y;
        public long time;
        public int mes;

        public C2CMessage(int id, float x, float y, long time, int mes=0)
        {
            this.id = id;
            this.x = x;
            this.y = y;
            this.time = time;
            this.mes = mes;
        }
    }
}
